unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, IniFiles;

type
  TForm2 = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  Dir: String;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Edit2.Text:=OpenDialog1.FileName;
end;

procedure TForm2.Button2Click(Sender: TObject);
var
  Ini:TIniFile;
  Path:String;
  F:File;
  A:array [0..255] of Char;
  i:integer;
begin
  if Edit2.Text='' then
  begin
    Beep;
    Exit;
  end;

  if Edit1.Text='' then
    Path:=ExtractFileName(Edit2.Text)
  else
  begin
     if (Pos(':',Edit1.Text)=0) or (Pos(':',Edit1.Text)=Length(Edit1.Text)) then
     begin
      Beep;
      exit;
     end;
     Path:=Copy(Edit1.Text,0,Pos(':',Edit1.Text)-1)+'.'+
          Copy(Edit1.Text,Pos(':',Edit1.Text)+1,Length(Edit1.Text)-Pos(':',Edit1.Text));
  end;

  if not FileExists(Dir+'spacks\'+Path) then
  begin
    if not MoveFile({PWide}PChar(Edit2.Text),{PWide}PChar(Dir+'spacks\'+Path)) then
    begin
      ShowMessage('Unable to move the key file. Already exists or locked.');
      exit;
    end;
  end
  else
    ShowMessage('The key is already exists. Go and delete existing key!');

  if Edit1.Text='' then
  begin
    for i := Length(ExtractFileName(Edit2.Text)) downto 1 do
      if ExtractFileName(Edit2.Text)[i]='.' then
      begin
        Edit1.Text:=Copy(ExtractFileName(Edit2.Text),0,i-1)+':'+
              Copy(ExtractFileName(Edit2.Text),i+1,
              Length(ExtractFileName(Edit2.Text))-i);
        break;
      end;
  end;

  Ini:=TINIFile.Create(Dir+'db.ini');
  Ini.WriteString('suidport_spack',Edit1.Text,Path);
  Ini.WriteString('suidport_spack',Edit1.Text,Path);
  Ini.Free;

  Form1.lvRepaint;
  Close;
end;

procedure TForm2.Button3Click(Sender: TObject);
begin
  Form2.Close;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  Dir:=ExtractFilePath(paramstr(0));
end;

end.
